package top.zywork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import top.zywork.constant.CommonConstant;
import top.zywork.dos.UserCertificationDO;
import top.zywork.dto.PagerDTO;
import top.zywork.enums.CertificationTypeEnum;
import top.zywork.enums.CertificationCheckStatusEnum;
import top.zywork.exception.GlobalException;
import top.zywork.mapper.UserCertificationMapper;
import top.zywork.query.UserCertificationQuery;
import top.zywork.vo.wx.UserCertificationVo;
import top.zywork.wx.save.UserCertificationSave;
import top.zywork.security.SecurityUtils;
import top.zywork.service.UserCertificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.wx.update.UserCertificationUpdate;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户实名认证表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-10-18
 */
@Service
public class UserCertificationServiceImpl extends ServiceImpl<UserCertificationMapper, UserCertificationDO> implements UserCertificationService {

    @Override
    public ResponseStatusVO save(UserCertificationSave userCertificationSave) {
        long userId = SecurityUtils.getJwtUser().getUserId();
        UserCertificationDO result = baseMapper.selectOne(new QueryWrapper<UserCertificationDO>()
                .eq("is_active", CommonConstant.IS_ACTIVE_TRUE)
                .eq("user_id", userId));
        if(result != null){
            return ResponseStatusVO.ok("已存在认证信息",null);
        }
        check(userCertificationSave);
        UserCertificationDO userCertificationDO = new UserCertificationDO();
        BeanUtils.copyProperties(userCertificationSave,userCertificationDO);
        userCertificationDO.setUserId(userId);
        userCertificationDO.setCheckStatus(CertificationCheckStatusEnum.TO_BE_CERTIFIED.getCode());
        userCertificationDO.setCreateTime(new Date());
        userCertificationDO.setIsActive(CommonConstant.IS_ACTIVE_TRUE);
        userCertificationDO.setUpdateTime(new Date());
        userCertificationDO.setCheckDes("认证中");
        baseMapper.insert(userCertificationDO);
        return ResponseStatusVO.ok("提交成功认证时间1-3天",null);
    }

    @Override
    public ResponseStatusVO update(UserCertificationUpdate userCertificationUpdate) {
        long userId = SecurityUtils.getJwtUser().getUserId();

        // 校验当前认证是否是当前用户 待审核、认证失败可以修改
        UserCertificationDO userCertificationDO = baseMapper.selectOne(new QueryWrapper<UserCertificationDO>()
                .eq("is_active", CommonConstant.IS_ACTIVE_TRUE)
                .eq("id",userCertificationUpdate.getId())
                .eq("user_id",userId)
                .ne("check_status", CertificationCheckStatusEnum.AUTHENTICATION_FAILED.getCode()));

        if(userCertificationDO ==null){
            return ResponseStatusVO.error("非法修改",null);
        }

        UserCertificationSave userCertificationSave = new UserCertificationSave();
        BeanUtils.copyProperties(userCertificationUpdate,userCertificationSave);
        userCertificationSave.setUserId(userId);
        check(userCertificationSave);

        // 更新信息
        UserCertificationDO update = new UserCertificationDO();
        BeanUtils.copyProperties(userCertificationUpdate,update);
        update.setCheckDes("认证中");
        update.setUpdateTime(new Date());
        update.setCheckStatus(1);
        baseMapper.updateById(update);
        return ResponseStatusVO.ok("更新成功",null);
    }

    @Override
    public UserCertificationVo info(Long id) {
        long userId = SecurityUtils.getJwtUser().getUserId();
        UserCertificationDO userCertificationDO = baseMapper.selectOne(new QueryWrapper<UserCertificationDO>().eq("user_id", userId)
                .eq("check_status", CertificationCheckStatusEnum.CERTIFIED.getCode())
                .eq("id", id)
                .eq("is_active", CommonConstant.IS_ACTIVE_TRUE));
        UserCertificationVo userCertificationVo = new UserCertificationVo();
        if(userCertificationDO!=null){
            BeanUtils.copyProperties(userCertificationDO,userCertificationVo);
        }
        return userCertificationVo;
    }

    private void check(UserCertificationSave userCertificationSave){
        // 查询认证类型的真实性
//        CertificationTypeEnum anEnum = CertificationTypeEnum.getEnum(userCertificationSave.getType());
//        if(anEnum == null){
//            throw new GlobalException("认证异常");
//        }
        // 校验卡号是否已存在
        UserCertificationDO card_number = baseMapper.selectOne(new QueryWrapper<UserCertificationDO>()
                .eq("card_number", userCertificationSave.getCardNumber())
                .eq("is_active", CommonConstant.IS_ACTIVE_TRUE)
                .eq("check_status", CertificationCheckStatusEnum.CERTIFIED.getCode())
        .ne(userCertificationSave.getUserId()!=null,"user_id",userCertificationSave.getUserId()));
        if(card_number != null){
            throw new GlobalException("卡号已被认证");
        }
    }


    @Override
    public PagerDTO listAllByCondition(UserCertificationQuery userCertificationQuery) {
        List<Object> objects = baseMapper.listAllByCondition(userCertificationQuery);
        Long aLong = baseMapper.countByCondition(userCertificationQuery);
        return PagerDTO.builder().rows(objects).total(aLong).build();
    }

    @Override
    public PagerDTO listPageByCondition(UserCertificationQuery userCertificationQuery) {
        List<Object> objects = baseMapper.listPageByCondition(userCertificationQuery);
        Long aLong = baseMapper.countByCondition(userCertificationQuery);
        return PagerDTO.builder().rows(objects).total(aLong).build();
    }
}
